<?php
/**
 * Created by JetBrains PhpStorm.
 * User: miaodb
 * Date: 11-10-28
 * Time: 下午3:36
 * To change this template use File | Settings | File Templates.
 */
 
class CLI_Updaterate2 extends CLI
{


    /**
     * 脚本主方法
     *
     * @return void
     */
    public function main()
    {
        $select = DB::select('item_id')->from('tc_rate')->group_by('item_id');
        $iterator = new Db_Iterator($select, 'default', 10);
        $i = 0;
        while ($iterator->valid())
        {
            // 遍历数据
			foreach ($iterator->current() as $order_rate)
            {
                    $item_id = $order_rate['item_id'];
                    $rate_count = DB::select()->from('tc_rate')->where('item_id', '=', $item_id)->where('role', '=', 0)->count_all();
                    $trade_count = DB::select()->from('tc_item_trade')->where('item_id','=',$item_id)->count_all();
                if($rate_count>$trade_count)
                {
                    $count = $rate_count - $trade_count;
//                    echo $count."\n";
//                    echo "delete from tc_rate where item_id=$item_id and  role=0 and trade_id=0 limit $count ";die;
                    mysql_query("delete from tc_rate where item_id=$item_id and  role=0 and trade_id=0 limit $count ");
                    $num = mysql_affected_rows();
                    if($num<$count)
                    {
                        $count = $count - $num;
                        mysql_query("delete from tc_rate where item_id=$item_id and  role=0  limit $count ");
                    }
                    echo '>';
                }

            }

                $iterator->next();
        }
        die;

        }

    }